--圆角矩形效果
local kernel = {}

kernel.category = "filter"
kernel.name = "roundedRect"
 
-- Shader code uses time environment333 variable CoronaTotalTime
 
kernel.vertexData =
{
    {
        name = "radius",    --圆角半径
        default = 0.3, 
        min = 0.001,
        max = 1,
        index = 0,  -- This corresponds to "CoronaVertexUserData.x"
    },
    {
        name = "wipeWidth",     --圆角边界模糊宽度
        default = 0.01, 
        min = 0.001,
        max = 0.1,
        index = 1,  
    },
}


kernel.fragment = [[
    //判断点是否在角落 0.是 1.否
   P_UV vec4 getIsCorner(P_UV vec2 new_uv, P_UV vec2 o1, P_UV vec2 o2, P_UV vec2 o3, P_UV vec2 o4)
   {
        P_UV float disX1 = step(o1.x, new_uv.x);
        P_UV float disY1 = step(o1.y, new_uv.y);
        P_UV float isCorner1 = step(1.0,disX1+disY1);

        P_UV float disX2 = step(o2.x, new_uv.x);
        P_UV float disY2 = step(o2.y, new_uv.y);
        P_UV float isCorner2 = step(0.0,disX2 - disY2);

        P_UV float disX3 = step(o3.x, new_uv.x);
        P_UV float disY3 = step(o3.y, new_uv.y);
        P_UV float isCorner3 = 1.0 - step(2.0,disX3 + disY3);

        P_UV float disX4 = step(o4.x, new_uv.x);
        P_UV float disY4 = step(o4.y, new_uv.y);
        P_UV float isCorner4 = 1.0 - step(1.0,disX4 - disY4);
        P_UV vec4 isCorner = vec4(isCorner1, isCorner2, isCorner3, isCorner4);
        return isCorner;
    }

    P_COLOR vec4 drawTex(P_UV vec2 uv)
    {	
        P_UV float proportion = CoronaTexelSize.y/CoronaTexelSize.x; //图片实际的宽度与高度比值v
        //四个角圆心坐标
        P_UV vec2 o1 = vec2(CoronaVertexUserData.x, CoronaVertexUserData.x);
        P_UV vec2 o2 = vec2(CoronaVertexUserData.x, 1.0 - CoronaVertexUserData.x);
        P_UV vec2 o3 = vec2(1.0*proportion - CoronaVertexUserData.x, 1.0 - CoronaVertexUserData.x);
        P_UV vec2 o4 = vec2(1.0*proportion - CoronaVertexUserData.x, CoronaVertexUserData.x);
        P_UV vec2 new_uv = vec2(uv.x*proportion,uv.y);

        //与四个圆心的距离
        P_UV vec4 disCorner = vec4(distance(o1, new_uv), distance(o2, new_uv), distance(o3, new_uv), distance(o4, new_uv));
        //判断点是否在角落
        P_UV vec4 isCorner = getIsCorner(new_uv, o1,o2,o3,o4);
        //根据圆心距离计算alpha
        P_UV vec4 a1 = vec4(1.0 - smoothstep(o1.x,o1.x + CoronaVertexUserData.y,disCorner.x), 1.0 - smoothstep(o1.x,o1.x + CoronaVertexUserData.y,disCorner.y),1.0 - smoothstep(o1.x,o1.x + CoronaVertexUserData.y,disCorner.z),1.0 - smoothstep(o1.x,o1.x + CoronaVertexUserData.y,disCorner.w));
        //根据是否在角落计算alpha
        P_UV vec4 a2 = vec4(max(a1.x, isCorner.x),max(a1.y, isCorner.y),max(a1.z, isCorner.z),max(a1.w, isCorner.w));
        P_COLOR vec4 texColor = texture2D( CoronaSampler0, uv);

        texColor.a = min(min(a2.x,a2.y),min(a2.z,a2.w));
        texColor.rgb *= texColor.a;

        return texColor;
    }

        P_COLOR vec4 FragmentKernel( P_UV vec2 texCoord )
    {
        P_UV vec2 uv = texCoord.xy; //- vec2(0.5,0.5) ;
        return CoronaColorScale(drawTex(uv));
    }
]]

if not globalGroup.shaderRecoder[kernel.name] then
    graphics.defineEffect(kernel)
    globalGroup.shaderRecoder[kernel.name] = true
end
